home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / bbedit-30-dev-kit.hqx / BBEdit 3.0 Extension Dev. Kit / Examples / ProjStats.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-03  |  3.2 KB  |  183 lines

  1. #include <SetupA4.h>
  2. #include <StandardFile.h>
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include "DialogUtilities.h"
  8. #include "ExternalInterface.h"
  9.  
  10. enum
  11. {
  12.     source_files,
  13.     not_found,
  14.     
  15.     neither_tree,
  16.     project_tree,
  17.     system_tree,
  18.  
  19.     total_counts,
  20.     
  21.     //    string list indexes
  22.     prj_info,
  23.     prj_files,
  24.     prj_lines,
  25.     file_summary,
  26.     file_name,
  27.     file_lines
  28. };
  29.  
  30. static long count_lines(ExternalCallbackBlock *callbacks, const FSSpec *spec)
  31. {
  32.     Boolean dispose;
  33.     Handle text;
  34.     
  35.     long    line_count = 0;
  36.     
  37.     text = callbacks->GetFileText(spec->vRefNum, spec->parID, *(Str255 *)spec->name, &dispose);
  38.  
  39.     if (text != nil)
  40.     {
  41.         char     *p = *text;
  42.         long    len = GetHandleSize(text);
  43.         
  44.         while (--len)
  45.         {
  46.             if (*(p++) == '\r')
  47.                 line_count++;
  48.         }
  49.         
  50.         if ((*text)[len - 1] != '\r')
  51.             line_count++;
  52.             
  53.         if (dispose)
  54.             DisposHandle(text);
  55.     }
  56.         
  57.     return line_count;    
  58. }
  59.  
  60. #define OUTPUT(fmt, arg) \
  61. {\
  62.     GetIndString(fmt_str, 128, fmt + 1);\
  63.     PtoCstr(fmt_str);\
  64.     sprintf(output_str, (char *)fmt_str, arg);\
  65.     err = PtrAndHand(output_str, output_text, strlen(output_str));\
  66. }\
  67.  
  68.  
  69. pascal void main(ExternalCallbackBlock *callbacks, WindowPtr w)
  70. {
  71.     StandardFileReply    reply;
  72.     ProjectEntry **entries;
  73.     short    type;
  74.     short    count;
  75.     
  76.     OSErr    err = noErr;
  77.     short    progress_counter = 0;
  78.     
  79.     RememberA0();
  80.     SetUpA4();
  81.     
  82.     StandardGetFile(nil, 3, *(SFTypeList *)"PROJQPRJMMPR", &reply);
  83.     if (reply.sfGood && callbacks->GetProjectList(&reply.sfFile, &type, &count, &entries))
  84.     {
  85.         short    counts[total_counts];
  86.         
  87.         short    i;
  88.         long    total_line_count;
  89.         
  90.         Str255    fmt_str;
  91.         char    output_str[512];
  92.         
  93.         
  94.         Handle    output_text;
  95.  
  96.         for (i = 0; i < total_counts; i++)
  97.             counts[i] = 0;
  98.             
  99.         total_line_count = 0;
  100.         
  101.         HLockHi((Handle)entries);
  102.         
  103.         callbacks->StartProgress("\pCounting Project…", count * 2, FALSE);
  104.         progress_counter = 0;
  105.         
  106.         for (i = 0; i < count; i++)
  107.         {
  108.             ProjectEntry entry = (*entries)[i];
  109.             
  110.             if (entry.found)
  111.             {
  112.                 counts[neither_tree + entry.tree]++;
  113.                 
  114.                 if (entry.type == 'TEXT')
  115.                 {
  116.                     counts[source_files]++;
  117.                     
  118.                     //    use the "crtr" field to store the line count for this file.
  119.                     //    It comes in handy later...
  120.                     total_line_count += ((*entries)[i].crtr = count_lines(callbacks, &entry.spec));
  121.                 }
  122.             }
  123.             else
  124.                 counts[not_found]++;
  125.                 
  126.             callbacks->DoProgress(progress_counter++);
  127.         }
  128.                 
  129.         output_text = callbacks->Allocate(0, FALSE);
  130.         
  131.         PtoCstr(reply.sfFile.name);
  132.         OUTPUT(prj_info, reply.sfFile.name);
  133.         
  134.         OUTPUT(prj_files, count);
  135.         OUTPUT(prj_lines, total_line_count);
  136.         
  137.         for (i = 0; (i < total_counts) && (err == noErr); i++)
  138.         {
  139.             if (counts[i] != 0)
  140.                 OUTPUT(i, counts[i]);
  141.         }
  142.         
  143.         OUTPUT(file_summary, "---");
  144.         
  145.         for (i = 0; i < count; i++)
  146.         {
  147.             ProjectEntry entry = (*entries)[i];
  148.             
  149.             if (entry.found && (entry.type == 'TEXT'))
  150.             {
  151.                 PtoCstr(entry.spec.name);
  152.                 
  153.                 OUTPUT(file_name, entry.spec.name);
  154.                 if (err != noErr)
  155.                     break;
  156.                     
  157.                 OUTPUT(file_lines, entry.crtr);
  158.                 if (err != noErr)
  159.                     break;
  160.             }
  161.             
  162.             callbacks->DoProgress(progress_counter++);
  163.         }
  164.         
  165.         callbacks->DoneProgress();
  166.         
  167.         if (err == noErr)
  168.         {
  169.             w = callbacks->NewDocument();
  170.             callbacks->SetWindowContents(w, output_text);
  171.         }
  172.         else
  173.         {
  174.             DisposHandle(output_text);
  175.             callbacks->ReportOSError(err);
  176.         }
  177.         
  178.         DisposHandle((Handle)entries);
  179.     }
  180.     
  181.     RestoreA4();
  182. }
  183.